package cn.kgc.tangcco.controller;

import cn.binarywang.tools.generator.ChineseIDCardNumberGenerator;
import cn.binarywang.tools.generator.ChineseNameGenerator;
import cn.kgc.tangcco.model.ResponseText;
import cn.kgc.tangcco.model.ResultCode;
import cn.kgc.tangcco.pojo.Person;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/person")
public class PersonHandler {
    private static List<Person> persons = new ArrayList<>();

    static {
        for (int i = 0; i < 5; i++) {
            persons.add(new Person((i + 1), ChineseNameGenerator.getInstance().generate(), ChineseIDCardNumberGenerator.getInstance().generate()));
        }
    }

    // @RequestMapping(value = "/test",method = RequestMethod.GET)
    @GetMapping(value = "/test")
    public String test() {
        return "桃李不言";
    }

    @GetMapping(value = "/query")
    public ResponseText<List<Person>> query() {
        return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), persons);
    }

    @PutMapping(value = "/add")
    public ResponseText add(Person person) {
        person.setId(persons.get(persons.size() - 1).getId() + 1);
        if (persons.add(person)) {
            return new ResponseText(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
        }
        return new ResponseText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
    }

    @DeleteMapping(value = "/delete")
    public ResponseText delete(@RequestBody Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            int finalI = i;
            persons.removeIf(person -> person.getId() == ids[finalI]);
        }
        return new ResponseText(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
    }

    @GetMapping(value = "/info/{id}")
    public ResponseText info(@PathVariable("id") Integer id) {
        if (id == null) {
            return new ResponseText(ResultCode.PARAM_ERROR.getCode(), ResultCode.DATA_ERROR.getMsg(), null);
        }
        List<Person> personList = persons.stream().filter(emp -> emp.getId() == id).collect(Collectors.toList());
        if (personList != null && personList.size() > 0) {
            return new ResponseText<Person>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), personList.get(0));
        }
        return new ResponseText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
    }

    @PutMapping(value = "/save")
    public ResponseText save(Person person) {
        if (person == null || person.getId() == null) {
            return new ResponseText(ResultCode.PARAM_ERROR.getCode(), ResultCode.DATA_ERROR.getMsg(), null);
        }
        for (int i = 0; i < persons.size(); i++) {
            if (persons.get(i).getId() == person.getId()) {
                persons.set(i, person);
                return new ResponseText<Person>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
            }
        }
        return new ResponseText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
    }
}
